package servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.DAO;

import model.FBData;
import model.User;

import com.google.gson.Gson;

/**
 * Servlet implementation class FBUserDetail
 */


@WebServlet("/FBUserDetail")
public class FBUserDetail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FBUserDetail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String access_token=request.getParameter("access_token");
		String FQL = "SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())";
		FQL = URLEncoder.encode(FQL);
		String URL3 = "https://graph.facebook.com/fql?q=" + FQL
				+ "&access_token=" + access_token;
		String data = "";
		String inputLine;
		URL url = new URL(URL3);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
		while ((inputLine = in.readLine()) != null)
			data = data + inputLine;
		in.close();
		System.out.println(data);
		Gson gson = new Gson();
		FBData fbd = gson.fromJson(data, FBData.class);
		request.setAttribute("access_token", access_token);
		request.setAttribute("data", fbd.getData());
		getServletContext().getRequestDispatcher("/view/users.jsp").forward(request, response); 

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
